package com.ardf.controller;

import com.ardf.entity.dto.BatchMenuDto;
import com.ardf.entity.dto.BatchTablePersonDto;
import com.ardf.entity.dto.BatchTableRelayDto;
import com.ardf.entity.dto.CanExchangeDto;
import com.ardf.entity.vo.DoPersonBatchVo;
import com.ardf.entity.vo.UpdateBatchVo;
import com.ardf.service.IBatchService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/batch")
public class BatchController {
    private final IBatchService batchService;

    public BatchController(IBatchService batchService) {
        this.batchService = batchService;
    }

    /**
     * 获取批次菜单
     */
    @GetMapping("/getMenu")
    public ResponseEntity<List<BatchMenuDto>> getMenu(@RequestParam("competitionId") Long competitionId) {
        List<BatchMenuDto> batchMenuDtos = batchService.getMenu(competitionId);
        return ResponseEntity.ok(batchMenuDtos);
    }

    /**
     * 搜索接力查询
     */
    @GetMapping("/searchAllRelay")
    public ResponseEntity<Page<BatchTableRelayDto>> searchAllRelay(@RequestParam("pageSize") Integer pageSize,
                                                                   @RequestParam("pageNum") Integer pageNum,
                                                                   @RequestParam("projectId") Long projectId,
                                                                   @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<BatchTableRelayDto> page;
        if (searchAll != null && StringUtils.hasLength(searchAll.trim())) {
            page = batchService.searchAllRelay(pageSize, pageNum, projectId, searchAll.trim());
        } else {
            page = batchService.searchAllRelay(pageSize, pageNum, projectId, null);
        }
        return ResponseEntity.ok(page);
    }

    /**
     * 搜索个人查询
     */
    @GetMapping("/searchAllPerson")
    public ResponseEntity<Page<BatchTablePersonDto>> searchAllPerson(@RequestParam("pageSize") Integer pageSize,
                                                                     @RequestParam("pageNum") Integer pageNum,
                                                                     @RequestParam("projectId") Long projectId,
                                                                     @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<BatchTablePersonDto> page;
        if (searchAll != null && StringUtils.hasLength(searchAll.trim())) {
            page = batchService.searchAllPerson(pageSize, pageNum, projectId, searchAll.trim());
        } else {
            page = batchService.searchAllPerson(pageSize, pageNum, projectId, null);
        }
        return ResponseEntity.ok(page);
    }
    /**
     * 进行个人分批
     */
    @PostMapping("/doPersonBatch")
    public void doPersonBatch(@RequestBody DoPersonBatchVo doPersonBatchVo){
        batchService.doRandomPersonBatch(doPersonBatchVo);
    }
    /**
     * 进行接力分批
     */
    @PostMapping("/doRelayBatch")
    public void doRelayBatch(@RequestParam("projectId") Long projectId){
        batchService.doRelayBatch(projectId);
    }
    @GetMapping("/getExchange")
    public ResponseEntity<List<CanExchangeDto>> getExchange(@RequestParam("batchId") Long batchId){
        return ResponseEntity.ok(batchService.getExchange(batchId));
    }


    /**
     * 调换批次
     */
    @PostMapping("/update")
    public void updateBatch(@RequestBody UpdateBatchVo updateBatchVo){
        batchService.updateBatch(updateBatchVo);
    }
    /**
     * 按批次号导出批次表
     */
    @GetMapping("/downloadBySequence")
    public void downloadBySequence(HttpServletResponse response, @RequestParam("projectId") Long projectId) throws IOException {
        batchService.downloadBySequence(response,projectId);
    }
    /**
     * 按参赛单位导出批次表
     */
    @GetMapping("/downloadByUnit")
    public void downloadByUnit(HttpServletResponse response, @RequestParam("projectId") Long projectId) throws IOException {
        batchService.downloadByUnit(response,projectId);
    }
}
